#include "Base64.h"


// 编码表
const char CBase64::s_encodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

//解码表
const char CBase64::s_decodeTable[] =
{
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        62, // '+'
        0, 0, 0,
        63, // '/'
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
        0, 0, 0, 0, 0, 0, 0,
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
        13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
        0, 0, 0, 0, 0, 0,
        26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
        39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
};

/*
** 编码
** param [in] pSrcData: 原始数据
** param [in] iSrcSize: 原始数据大小
** param [out] strDst:  编码后的数据
*/
void CBase64::Encode(const unsigned char* pSrcData,int iSrcSize,std::string& strDst)
{
    strDst.clear();

    unsigned char szTmp[4] = {0};
    int iLineLength = 0;

    for(int i = 0; i < (int)(iSrcSize/3); ++i)
    {
        szTmp[1] = *pSrcData++;
        szTmp[2] = *pSrcData++;
        szTmp[3] = *pSrcData++;
        strDst += s_encodeTable[szTmp[1] >> 2];
        strDst += s_encodeTable[((szTmp[1] << 4) | (szTmp[2] >> 4)) & 0x3F];
        strDst += s_encodeTable[((szTmp[2] << 2) | (szTmp[3] >> 6)) & 0x3F];
        strDst += s_encodeTable[szTmp[3] & 0x3F];
        iLineLength += 4;
        if( iLineLength == 76 )
        {
            strDst += "\r\n";
            iLineLength = 0;
        }
    }

    // 对剩余数据进行编码
    switch ( iSrcSize % 3 )
    {
        case 1 :
        {
            szTmp[1] = *pSrcData++;
            strDst += s_encodeTable[(szTmp[1] & 0xFC) >> 2];
            strDst += s_encodeTable[((szTmp[1] & 0x03) << 4)];
            strDst += "==";
            break;
        }
        case 2 :
        {
            szTmp[1] = *pSrcData++;
            szTmp[2] = *pSrcData++;
            strDst += s_encodeTable[(szTmp[1] & 0xFC) >> 2];
            strDst += s_encodeTable[((szTmp[1] & 0x03) << 4) | ((szTmp[2] & 0xF0) >> 4)];
            strDst += s_encodeTable[((szTmp[2] & 0x0F) << 2)];
            strDst += "=";
            break;
        }
        default :
        {
            break;
        }
    }
}

/*
** 解码
** param [in] pSrcData:  原始数据
** param [in] iSrcSize:  原始数据大小
** param [out] strDst:   解码后的数据
** param [out] iDstSize: 解码后的数据大小
** Note:解码后的大小不要通过string.size()计算
*/
void CBase64::Decode(const char* pSrcData,int iSrcSize,std::string& strDst,int& iDstSize)
{
    iDstSize = 0;
    strDst.clear();

    int iValue = 0,i = 0;
    while( i < iSrcSize )
    {
        if(*pSrcData != '\r' && *pSrcData != '\n')
        {
            iValue = s_decodeTable[int(*pSrcData++)] << 18;
            iValue += s_decodeTable[int(*pSrcData++)] << 12;
            strDst += (iValue & 0x00FF0000) >> 16;
            ++iDstSize;
            if(*pSrcData != '=')
            {
                iValue += s_decodeTable[int(*pSrcData++)] << 6;
                strDst += (iValue & 0x0000FF00) >> 8;
                ++iDstSize;
                if(*pSrcData != '=')
                {
                    iValue += s_decodeTable[int(*pSrcData++)];
                    strDst += iValue & 0x000000FF;
                    ++iDstSize;
                }
            }
            i += 4;
        }
        else // 回车换行,跳过
        {
            pSrcData++;
            ++i;
        }
    }
}
